﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RescueApp.Data;
using EFCoreRepository.Extensions;
using RescueApp.Model;
using Microsoft.Data.SqlClient;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using RescueAPP.UI.Common;

namespace RescueApp.UI
{
    public partial class DispatchCar : Form
    {
        public static string ConnectingStr = "Server=LAPTOP-1BOQI73G;Database=Rescue;TrustServerCertificate=True;Integrated Security=True;";
        RescueContext db = new RescueContext(ConnectingStr);

        // private RescueContext db;
        public DispatchCar()
        {
            InitializeComponent();
        }

        public DispatchCar(RescueContext db)
        {
            InitializeComponent();
            this.db = db;

        }

        private void DispatchCar_Load(object sender, EventArgs e)
        {
            loadData(dataGridView1);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            AddRescueDispatch frm = new AddRescueDispatch(db);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                DispatchCar_Load(this, null);
            }
            frm.Close();
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            loadData(dataGridView1);
        }

        private void loadData(DataGridView dg)
        {
            List<RescueDispatch> todoItems = db.RescueDispatches.ToList<RescueDispatch>();
            dg.DataSource = todoItems;
            dg.Refresh();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            int idx;
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择你想要修改的数据！", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
                return;
            }
            idx = dataGridView1.SelectedRows[0].Index;
            int Id = int.Parse(dataGridView1.Rows[idx].Cells[0].Value.ToString());

            var todoitem = db.RescueDispatches.FirstOrDefault(x => x.DispatchID == Id);
            if (todoitem == null)
            {
                MessageBox.Show($"not Found the Id:{Id} Item!");
                return;
            }

            ChangeRescueDispatch frm = new ChangeRescueDispatch(db, todoitem);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                DispatchCar_Load(this, null);
            }
            frm.Close();

        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            int idx;
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择你想要修删除的数据！", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            idx = dataGridView1.SelectedRows[0].Index;
            int Id = int.Parse(dataGridView1.Rows[idx].Cells[0].Value.ToString());
            if (MessageBox.Show($"确定要删除派遣单ID:{Id} 吗?", "确认消息", MessageBoxButtons.OKCancel) != DialogResult.OK)
            {
                return;
            }
            if (!DbHelper.IsExistsWithORM2(db, Id))
            {
                MessageBox.Show($"该派遣单ID:{Id} 不存在!");
                return;
            }
            var entity = db.RescueDispatches.FirstOrDefault(x => x.DispatchID == Id);
            if (entity != null)
            {
                try
                {
                    db.RescueDispatches.Remove(entity);
                    db.SaveChanges();
                    MessageBox.Show($"派遣单ID:{Id} 删除成功!");
                    DispatchCar_Load(this, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void RescueCar_Load_1(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            ResultRescue frm = new ResultRescue();
            frm.ShowDialog();
        }
    }
}